﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace DataAccess
{
    public partial class DataAccess
    {
        public DataAccessResult GetDeploymentProperty(String SubscriptionID,String CertificatePath,String ServiceName,String DeploymentSlot)
        {
            DataAccessResult result = new DataAccessResult();

            string requestUrl = "https://management.core.windows.net/" + SubscriptionID + "/services/hostedservices/"+ServiceName +"/deploymentslots/"+DeploymentSlot;
            string ReturnBody = string.Empty;
            WebResponse resp = null;
            try
            {
                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);

                X509Certificate cert = X509Certificate.CreateFromCertFile(CertificatePath);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "GET";
                request.ClientCertificates.Add(cert);
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }
                resp = request.GetResponse();
                if (resp != null)
                {
                    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), true))
                    {
                        ReturnBody = sr.ReadToEnd();
                        sr.Close();
                    }
                    if (ReturnBody != null)
                    {
                        Entities.DeploymentProperty deploymentProperty = new Entities.DeploymentProperty();
                        XElement el = XElement.Parse(ReturnBody);
                        IEnumerable<XElement> allElements = el.Elements();
                        foreach (XElement element in allElements)
                        {
                            string ElementName = element.Name.LocalName.Trim().ToLower();
                            string ElementValue = element.Value;
                            switch (ElementName)
                            {
                                case "name":
                                    deploymentProperty.Name = ElementValue;
                                    break;
                                case "privateid":
                                    deploymentProperty.PrivateID = ElementValue;
                                    break;
                                case "label":
                                    byte[] encodedDataAsBytes = System.Convert.FromBase64String(ElementValue);
                                    deploymentProperty.Label = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes); 
                                    break;
                                case "status":
                                    deploymentProperty.Status = ElementValue;
                                    break;
                                case "configuration":
                                    deploymentProperty.Configuration = ElementValue;
                                    break;
                                case "deploymentslot":
                                    deploymentProperty.DeploymentSlot = ElementValue;
                                    break;
                                case "url":
                                    deploymentProperty.Url = ElementValue;
                                    break;
                                case "upgradestatus":
                                    foreach (XElement e2 in element.Elements())
                                    {
                                        string ElementName1 = e2.Name.LocalName.Trim().ToLower();
                                        string ElementValue1 = e2.Value;
                                        switch (ElementName1)
                                        {
                                            case "upgradetype":
                                                deploymentProperty.UpgradeType = ElementValue1;
                                                break;
                                            case "currentupgradedomainstate":
                                                deploymentProperty.CurrentUpgradeDomainState = ElementValue1;
                                                break;
                                            case "currentupgradedomain":
                                                deploymentProperty.CurrentUpgradeDomain = ElementValue1;
                                                break;
                                        }
                                    }
                                    break;
                                case "roleinstancelist":
                                    List<Entities.RoleInstance> roleList = new List<Entities.RoleInstance>();
                                    foreach (XElement roleInstance in element.Elements())
                                    {
                                        Entities.RoleInstance role = new Entities.RoleInstance();
                                        foreach (XElement e3 in roleInstance.Elements())
                                        {
                                            string ElementName3 = e3.Name.LocalName.Trim().ToLower();
                                            string ElementValue3 = e3.Value;
                                            switch (ElementName3)
                                            {
                                                case "rolename":
                                                    role.RoleName = ElementValue3;
                                                    break;
                                                case "instancename":
                                                    role.InstanceName = ElementValue3;
                                                    break;
                                                case "instancestatus":
                                                    role.InstanceState = ElementValue3;
                                                    break;
                                            }
                                        }
                                        roleList.Add(role);
                                    }
                                    deploymentProperty.RoleInstanceList = roleList;
                                    break;
                            }
                        }
                        result.IsOperationSuccessful = true;
                        result.DeploymentProperty = deploymentProperty;
                    }
                }
            }
            catch (WebException webEx)
            {
                result.IsOperationSuccessful = false;
                result.DeploymentProperty = null;
                result.Error = webEx;
            }
            catch (Exception excep)
            {
                result.IsOperationSuccessful = false;
                result.DeploymentProperty = null;
                result.Error = excep;
            }
            finally
            {
                //req = null;
                if (resp != null) { resp.Close(); }
                resp = null;
            }
            return result;
        }
    }
}
